"""用于封装http请求"""

import requests
from utils.decorators import http_exception_catcher as error_catcher,logger
from config.env import URLDve



class HttpClient:
    def __init__(self,base_url,headers=None,timeout=10):
        """初始化 HttpClient 实例"""
        """
        base_url: API 的基础 URL
        headers: 默认的请求头信息，默认为空
        timeout: 请求超时时间，默认为 10 秒
        """
        self.base_url = base_url
        self.headers = headers or {}
        self.timeout = timeout


    @logger
    @error_catcher
    def get(self,endpoint,params=None,headers=None):
        """封装的 GET 请求方法"""
        """
        endpoint: API 的具体路径
        params: 请求参数，默认为空
        headers: 请求头信息，可覆盖默认头信息
        
        """
        url = f"{self.base_url}{endpoint}"
        request_headers = {**self.headers, **(headers or {})}
        #发送请求
        response = requests.get(url = url,params = params,headers=request_headers,timeout=self.timeout)

        return response


    @logger
    @error_catcher
    def post(self,endpoint,params=None,headers=None,data=None, json=None):
        """封装的 post 请求方法"""
        """
        endpoint: API 的具体路径
        params: 请求参数，默认为空
        headers: 请求头信息，可覆盖默认头信息

        """
        url = f"{self.base_url}{endpoint}"
        request_headers = {**self.headers, **(headers or {})}
        # 发送请求
        response = requests.post(url=url, params=params, headers=request_headers, timeout=self.timeout,data=data, json=json)

        return response

    @logger
    @error_catcher
    def patch(self,endpoint,params=None,headers=None):
        """封装的 patch 请求方法"""
        """
        endpoint: API 的具体路径
        params: 请求参数，默认为空
        headers: 请求头信息，可覆盖默认头信息

        """
        url = f"{self.base_url}{endpoint}"
        request_headers = {**self.headers, **(headers or {})}
        # 发送请求
        response = requests.patch(url=url, params=params, headers=request_headers, timeout=self.timeout)

        return response

    @logger
    @error_catcher
    def delete(self,endpoint,params=None,headers=None,data=None, json=None):
        """封装的 post 请求方法"""
        """
        endpoint: API 的具体路径
        params: 请求参数，默认为空
        headers: 请求头信息，可覆盖默认头信息

        """
        url = f"{self.base_url}{endpoint}"
        request_headers = {**self.headers, **(headers or {})}
        # 发送请求
        response = requests.delete(url=url, params=params, headers=request_headers, timeout=self.timeout,data = data, json=json)

        return response


    @logger
    @error_catcher
    def put(self,endpoint,params=None,headers=None):
        """封装的 post 请求方法"""
        """
        endpoint: API 的具体路径
        params: 请求参数，默认为空
        headers: 请求头信息，可覆盖默认头信息

        """
        url = f"{self.base_url}{endpoint}"
        request_headers = {**self.headers, **(headers or {})}
        # 发送请求
        response = requests.put(url=url, params=params, headers=request_headers, timeout=self.timeout)

        return response



if __name__ == '__main__':

    method = HttpClient(base_url=f'{URLDve.LLM}')
    print(method.get(endpoint="/sanji/v1/monitoring/health/"))